import axios from './http'
class AxiosWraper {
    constructor() {
        this.proxyKeys = [];
        this.getConfigProxyUrl();
    }
    getConfigProxyUrl(param) {
        let _t = this; 
        _t.proxyKeys = [];
        let key = param
        let res = axios.get(`/agsupport-rest/agsupport/projectManager/get?name=${key}`);
        res.then(function (data) {
            if (data.success) {
                let arr = JSON.parse(data.content).netConfig;
                for (let i = 0; i < arr.length; i++) {
                    if ("pre"===arr[i].key) {
                        let o = {
                            pre: arr[i].value
                        };
                        _t.proxyKeys.push(o);
                    }
                }
            }
        }); 
    }
    getProxyUrl(url) {
        var resUrl = url;
        for (var i = 0; i < this.proxyKeys.length; i++) {
            var tv = url.startsWith(this.proxyKeys[i].pre);
            if (!tv) continue;
            var turl = url.substring(this.proxyKeys[i].pre.length + 1);
            resUrl = `${turl}`;
            break;
        }
        return resUrl;
    }

    getData(url, params) {
        var resUrl = this.getProxyUrl(url);
        return axios.get(resUrl, {
            params: params
        });
    }
    getDataByPost(url, data, params) {
        var resUrl = this.getProxyUrl(url);
        return axios(
            {
                method: 'POST',
                url: resUrl,
                data: data,
                timeout: 1000 * 60 * 5,
            }
        );
    }

    uploadFile(url, file, id) {

    }
    /**
     * 获得兄弟文件(可以深度)的地址
     * @param {*} url 
     */
    getSibleFileUrl(url, fileName) {
        return url.substr(0, url.lastIndexOf('/')) + "/" + fileName;
    }
};

export default new AxiosWraper();